#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/10/12 17:55
# @Author : 闫峰
# @File    : ceshi.py
from datetime import datetime

import pymongo

from utils.mysqlUtils.mongodb_utils import MongoDBLink
import os

from common.setting import ensure_path_sep
from utils.logging_tool.log_control import LogHandler
from utils.mysqlUtils.db_pool import Database

current_path = os.path.abspath(__file__)
current_dir = os.path.dirname(current_path)
path_list = current_path.split("\\")
dis_path = "\\".join(path_list[-2:-1])
info_path = ensure_path_sep(f"\\logs\\info-{dis_path}.log")
error_path = ensure_path_sep(f"\\logs\\\error-{dis_path}.log")
warning_path = ensure_path_sep(f"\\logs\\warning-{dis_path}.log")
if os.path.exists(info_path):
    os.remove(info_path)
if os.path.exists(error_path):
    os.remove(error_path)
if os.path.exists(warning_path):
    os.remove(warning_path)
INFO = LogHandler(info_path, level='info')
ERROR = LogHandler(error_path, level='error')
WARNING = LogHandler(warning_path, level='warning')


class TestA:

    def test_(self):
        modb = MongoDBLink(
            uri='mongodb://rwuser:G1mob8U3VQ1obsFxHClJ!@124.71.9.215:8635,110.41.3.226:8635/?authSource=admin',
            dbname='irobotbox_marketpublish', coll_name='Ap_AutoPublish_Allegro_Listing')
        znkd_db = Database(ensure_path_sep("\\config\\znkd_mysql_config.yaml"), "znkd_database_prod")
        # 1 从mongodb筛选出要检查的数据
        start_date = datetime(2023, 10, 10, 0, 0, 0)
        filter = {
            "AddTime": {
                "$gte": start_date
            }
        }
        get_CustomValuesEnabled = """
        SELECT CustomValuesEnabled FROM m_allegro_attribute WHERE CustomerId=1 AND CategoryId='259567' AND `Name` LIKE %s AND Required=1
        """
        get_sku = """
        		SELECT DISTINCT
  a.Sku
 
FROM
  ap_autopublish_allegro_queue a
  inner JOIN sys_ordersource c ON a.CustomerId = c.CustomerId 
  AND a.CustomerId=c.CustomerId and a.OrderSourceId = c.OrderSourceId 
WHERE
  a.CustomerId = 1 
  and a.MarketId=67
  AND a.`Status` = 'failure' 
  AND a.Time >= '2023-10-16'
 -- AND a.FailureReason LIKE  'ConstraintViolationException.Uuid;afterSalesServices.impliedWarranty;This field contains an incorrect value.'
AND a.FailureReason ='DictionaryParameterValueNotUnique;productSet[0].product.parameters;The entered value `no brand` is not unique for the parameter `Brand`.'
ORDER BY Time DESC
	LIMIT 50
        """
        sku_list = znkd_db.fetch_all_tuple(get_sku,())
        for data in sku_list:
            a = '%Brand%'
            CustomValuesEnabled = znkd_db.fetch_all_tuple(get_CustomValuesEnabled,(data[0],a))
            print(CustomValuesEnabled)







if __name__ == '__main__':
    TestA().test_()
